package com.songcha.library_common.ui.view

import android.annotation.SuppressLint
import android.content.Context
import android.content.res.ColorStateList
import android.graphics.*
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import android.graphics.drawable.GradientDrawable.LINEAR_GRADIENT
import android.graphics.drawable.StateListDrawable
import android.os.Build
import android.util.AttributeSet
import android.view.Gravity
import android.widget.Button
import com.songcha.library_common.R
import com.songcha.library_common.util.AlgorithmUtil
import com.songcha.library_common.util.ColorUtil
import com.songcha.library_common.util.dp2px

@SuppressLint("AppCompatCustomView")
class CustomButton
@JvmOverloads
constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyle: Int = 0,
    defRes: Int = 0
) : Button (context,attrs,defStyle,defRes){
    companion object{
        private val DEFAULT_PADDING=10.dp2px()
        private val DEFAULT_TEXTSIZE=15f
    }

    private var mBackgroundColor=Color.WHITE
    private var mPressColor=Color.TRANSPARENT
    private var mDisableColor=Color.GRAY
   // var mRadius=0f
    private var mBorderWidth=0f
    private var mBorderColor=Color.TRANSPARENT
    private var mRadius=FloatArray(4)
    private var mRadii=FloatArray(8)
    private var mPath=Path()
    private var mTextColor=Color.WHITE
    private var mHasTextSize=false
    private var mHasTextColor=false

    private var mStartColor=Color.TRANSPARENT
    private var mMiddleColor=Color.TRANSPARENT
    private var mEndColor=Color.TRANSPARENT

    init {
        initView(context,attrs)
    }


    @SuppressLint("ResourceType")
    private fun initView(context: Context, attrs: AttributeSet?){
        includeFontPadding=false

        //var padding=-1
        if(attrs==null){
            mBackgroundColor=ColorUtil.getResourceColor(context,R.color.btn_color)
        }else{

            val ta=context.obtainStyledAttributes(attrs, R.styleable.CustomButton)
            mPressColor=ta.getColor(R.styleable.CustomButton_btn_press_color,Color.TRANSPARENT)
            mDisableColor=ta.getColor(R.styleable.CustomButton_btn_disable_color,ColorUtil.getResourceColor(context,R.color.btn_color_disable))
            mBorderWidth=ta.getDimension(R.styleable.CustomButton_btn_border_width,0f)
            mBorderColor=ta.getColor(R.styleable.CustomButton_btn_border_color,Color.WHITE)
            mStartColor=ta.getColor(R.styleable.CustomButton_btn_start_color,Color.TRANSPARENT)
            mMiddleColor=ta.getColor(R.styleable.CustomButton_btn_middle_color,Color.TRANSPARENT)
            mEndColor=ta.getColor(R.styleable.CustomButton_btn_end_color,Color.TRANSPARENT)

            //cannot get ref attr
            //mTextColor=attrs.getAttributeIntValue("http://schemas.android.com/apk/res/android","textColor",Color.WHITE)
            //val a=attrs.getAttributeResourceValue("http://schemas.android.com/apk/res/android","textColor",Color.WHITE)
            mHasTextColor=attrs.getAttributeValue("http://schemas.android.com/apk/res/android","textColor")!=null
            mHasTextSize=attrs.getAttributeValue("http://schemas.android.com/apk/res/android","textSize")!=null


            if(background!=null && background is ColorDrawable)
                mBackgroundColor=(background as ColorDrawable).color
            else{
                mBackgroundColor=ColorUtil.getResourceColor(context,R.color.btn_color)
            }
            /*val paddingArray= intArrayOf(android.R.attr.padding,android.R.attr.paddingLeft, android.R.attr.paddingTop, android.R.attr.paddingRight, android.R.attr.paddingBottom)
            val taPadding = context.obtainStyledAttributes(attrs, paddingArray)
            padding=taPadding.getDimensionPixelOffset(0,-1)
            val paddingLeft=taPadding.getDimensionPixelOffset(1,-1)
            val paddingTop=taPadding.getDimensionPixelOffset(2,-1)
            val paddingRight=taPadding.getDimensionPixelOffset(3,-1)
            val paddingBottom=taPadding.getDimensionPixelOffset(4,-1)
            taPadding.recycle()

            if(padding==-1 && paddingLeft==-1 && paddingTop==-1 && paddingRight==-1 && paddingBottom==-1){
                padding=-1
            }else{
                padding=0
            }*/

            val l=ta.getDimension(R.styleable.CustomButton_btn_left_radius,0f)
            val t=ta.getDimension(R.styleable.CustomButton_btn_top_radius,0f)
            val r=ta.getDimension(R.styleable.CustomButton_btn_right_radius,0f)
            val b=ta.getDimension(R.styleable.CustomButton_btn_bottom_radius,0f)
            val radius=ta.getDimension(R.styleable.CustomButton_btn_radius,0f)

            if(radius>0){
                mRadius[0]=radius
                mRadius[1]=radius
                mRadius[2]=radius
                mRadius[3]=radius
            }else{
                mRadius[0]=l
                mRadius[1]=t
                mRadius[2]=r
                mRadius[3]=b
            }

            ta.recycle()
        }

        //set height and padding simultaneously will cause font cannot in center
       /* if(padding==-1){
            setPadding(DEFAULT_PADDING,DEFAULT_PADDING,DEFAULT_PADDING,DEFAULT_PADDING)
        }*/

        gravity=Gravity.CENTER
        if(!mHasTextColor)
            setTextColor(Color.WHITE)
        if(!mHasTextSize){
            textSize=DEFAULT_TEXTSIZE
        }

        if(mPressColor==Color.TRANSPARENT){
            mPressColor=mBackgroundColor
        }

        val ta2 = context.obtainStyledAttributes(attrs, androidx.appcompat.R.styleable.AppCompatTheme)
        val f=ta2.getDrawable(androidx.appcompat.R.styleable.AppCompatTheme_selectableItemBackground)
        if(Build.VERSION.SDK_INT>=23){
            foreground=f
        }
        ta2.recycle()

        //initDrawable()


    }

    fun setStartColor(color:Int){
        mStartColor=color
        mBackgroundColor=Color.TRANSPARENT
        initDrawable()
    }

    fun setEndColor(color:Int){
        mEndColor=color
        mBackgroundColor=Color.TRANSPARENT
        initDrawable()
    }


    private fun initDrawable(){
        lateinit var mPressDrawable:Drawable
        lateinit var mBackgroundDrawable:Drawable
        lateinit var mDisableDrawable:Drawable

        if(mStartColor!=Color.TRANSPARENT && mMiddleColor!=Color.TRANSPARENT && mEndColor!=Color.TRANSPARENT){
            mPressDrawable=GradientDrawable()
            mPressDrawable.orientation=GradientDrawable.Orientation.LEFT_RIGHT
            mPressDrawable.colors=intArrayOf(mStartColor,mMiddleColor,mEndColor)
            mPressDrawable.setGradientType(LINEAR_GRADIENT)

            mBackgroundDrawable=GradientDrawable()
            mBackgroundDrawable.orientation=GradientDrawable.Orientation.LEFT_RIGHT
            mBackgroundDrawable.colors=intArrayOf(mStartColor,mMiddleColor,mEndColor)
            mBackgroundDrawable.setGradientType(LINEAR_GRADIENT)
        }else if(mStartColor!=Color.TRANSPARENT && mMiddleColor==Color.TRANSPARENT && mEndColor!=Color.TRANSPARENT){
            mPressDrawable=GradientDrawable()
            mPressDrawable.orientation=GradientDrawable.Orientation.LEFT_RIGHT
            mPressDrawable.colors=intArrayOf(mStartColor,mEndColor)
            mPressDrawable.setGradientType(LINEAR_GRADIENT)

            mBackgroundDrawable=GradientDrawable()
            mBackgroundDrawable.orientation=GradientDrawable.Orientation.LEFT_RIGHT
            mBackgroundDrawable.colors=intArrayOf(mStartColor,mEndColor)
            mBackgroundDrawable.setGradientType(LINEAR_GRADIENT)
        }else{
            mPressDrawable=GradientDrawable()
            mPressDrawable.setColor(mPressColor)

            mBackgroundDrawable=GradientDrawable()
            mBackgroundDrawable.setColor(mBackgroundColor)
        }

        mDisableDrawable=GradientDrawable()
        mDisableDrawable.setColor(mDisableColor)

        mRadii=AlgorithmUtil.formatRadiiFloatArray(mRadius)

        mPressDrawable.cornerRadii=mRadii
        mBackgroundDrawable.cornerRadii=mRadii
        mDisableDrawable.cornerRadii=mRadii

        if(mBorderWidth>0f && mBorderColor!=Color.TRANSPARENT){
            mPressDrawable.setStroke(mBorderWidth.toInt(),mBorderColor)
            mBackgroundDrawable.setStroke(mBorderWidth.toInt(),mBorderColor)
            mDisableDrawable.setStroke(mBorderWidth.toInt(),mBorderColor)
        }

        val sld=StateListDrawable()

        sld.addState(intArrayOf(android.R.attr.state_pressed,android.R.attr.state_enabled),mPressDrawable)
        sld.addState(intArrayOf(android.R.attr.state_enabled),mBackgroundDrawable)
        sld.addState(intArrayOf(-android.R.attr.state_enabled),mDisableDrawable)

        background = sld

    }

    fun setBtnBackgroundColor(color:Int){
        mBackgroundColor = color
        mPressColor = color
        mStartColor = Color.TRANSPARENT
        mEndColor = Color.TRANSPARENT
        initDrawable()
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
    }

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)
        initDrawable()
        val rect=RectF(0f,0f,measuredWidth.toFloat(),measuredHeight.toFloat())
        mPath=Path()
        mPath.addRoundRect(rect,mRadii,Path.Direction.CW)
    }

    override fun onDraw(canvas: Canvas) {
        canvas?.let {
            it.clipPath(mPath)
        }

        super.onDraw(canvas)

    }


}